package com.ylxl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxl.common.annotation.Log;
import com.ylxl.common.enums.BusinessType;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.result.BaseController;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.LaborProtection;
import com.ylxl.entity.reponse.LaborProtectionVO;
import com.ylxl.entity.request.LaborProtectionCmdOrMdy;
import com.ylxl.entity.request.QueryLaborProtection;
import com.ylxl.entity.request.StatusRequest;
import com.ylxl.service.ILaborProtectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 劳保配置
 * @Auther Gilbert
 * @Date 2023/6/25 18:53
 */
@Slf4j
@RestController
@RequestMapping("/laborProtection")
@Api(tags = "劳保配置", hidden = true)
public class LaborProtectionController extends BaseController {

    @Resource
    private ILaborProtectionService laborProtectionService;

    /**
     * 分页查询劳保配置列表信息
     * @param queryLaborProtection
     * @return
     */
    @Log(title = "分页查询劳保配置列表信息", businessType = BusinessType.SELECT)
    @PostMapping("/queryLaborProtectionList")
    @ApiOperation(value = "分页查询劳保配置列表信息", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = LaborProtectionVO.class),})
    public AjaxResult queryLaborProtectionList(@RequestBody QueryLaborProtection queryLaborProtection) {
        AjaxResult ajax = AjaxResult.success();
        try {
            //进行分页操作
            PageInfo<LaborProtectionVO> pageInfo = new PageInfo<>();
            if (StringUtils.isNotEmpty(queryLaborProtection.getPageNum()) && StringUtils.isNotEmpty(queryLaborProtection.getPageSize())) {
                PageHelper.startPage(queryLaborProtection.getPageNum(), queryLaborProtection.getPageSize());
            }
            List<LaborProtectionVO> laborProtectionVOList = laborProtectionService.selectLabelList(queryLaborProtection);
            pageInfo = new PageInfo<>(laborProtectionVOList);
            ajax.put(AjaxResult.DATA_TAG, laborProtectionVOList);
            ajax.put(AjaxResult.TOTAL, pageInfo.getTotal());
        } catch (Exception e) {
            log.error("查询劳保列表日志信息，异常信息：{}", e);
            return toAjax(false);
        }
        return ajax;
    }

    /**
     * 劳保配置保存
     * @param laborProtectionCmdOrMdy
     * @return
     */
    @Log(title = "劳保配置保存", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ApiOperation(value = "劳保配置保存", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult add(@RequestBody LaborProtectionCmdOrMdy laborProtectionCmdOrMdy){
        if (laborProtectionService.checkNameUnique(laborProtectionCmdOrMdy)){
            return error("新增劳保" + laborProtectionCmdOrMdy.getLaborName() + "'失败，劳保已存在");
        }
        return toAjax(laborProtectionService.insertLabel(laborProtectionCmdOrMdy));
    }

    /**
     * 劳保配置编辑
     * @param laborProtectionCmdOrMdy
     * @return
     */
    @Log(title = "劳保配置编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "劳保配置编辑", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult update(@RequestBody LaborProtectionCmdOrMdy laborProtectionCmdOrMdy){
        if (laborProtectionService.checkNameUnique(laborProtectionCmdOrMdy)){
            return error("新增劳保" + laborProtectionCmdOrMdy.getLaborName() + "'失败，劳保已存在");
        }
        return toAjax(laborProtectionService.updateLabel(laborProtectionCmdOrMdy));
    }
    /**
     * 劳保根据id查询明细
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "劳保根据id查询明细", response = AjaxResult.class)
    @Log(title = "劳保根据id查询明细", businessType = BusinessType.SELECT)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = LaborProtection.class),})
    public AjaxResult getInfo(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(laborProtectionService.selctById(id));
    }
    /**
     * 劳保根据id删除
     */
    @PostMapping("/remove/{id}")
    @ApiOperation(value = "劳保根据id删除", response = AjaxResult.class)
    @Log(title = "劳保根据id删除", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult remove(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(laborProtectionService.deleteById(id));
    }
    /**
     * 劳保启用或者禁用
     */
    @PostMapping("/enableOrDisable")
    @ApiOperation(value = "劳保启用或者禁用", response = AjaxResult.class)
    @Log(title = "劳保或者禁用", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult enableOrDisable(@RequestBody StatusRequest statusRequest) {
        return AjaxResult.success(laborProtectionService.enableOrDisable(statusRequest));
    }
}
